package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;

import com.wzx.basics.config.ConstantConfig;
import com.wzx.basics.log.LogType;
import com.wzx.basics.log.SystemLog;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.ResultUtil;

import com.wzx.data.entity.CourseWare;
import com.wzx.data.entity.StudentWork;
import com.wzx.data.service.IStudentWorkService;

import com.wzx.data.utils.FileUtils;
import com.wzx.data.vo.CourseWareVo;
import com.wzx.data.vo.StudentWorkVo;
import com.wzx.data.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * 学生作业 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "学生作业管理接口")
@RequestMapping("/wzx/studentWork")
@Transactional
public class StudentWorkController {

    @Autowired
    private IStudentWorkService iStudentWorkService;

    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save( StudentWork studentWork) {
        iStudentWorkService.save(studentWork);
        return ResultUtil.success();
    }

    @ApiOperation("根据Id获取")
    @GetMapping("getById/{id}")
    public Result get(@PathVariable String id) {
        StudentWork studentWork = iStudentWorkService.getById(id);
        return ResultUtil.data(studentWork);
    }

    @ApiOperation("修改")
    @PostMapping("update")
    public Result update( StudentWork studentWork) {
        iStudentWorkService.updateById(studentWork);
        return ResultUtil.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable String id) {
        iStudentWorkService.removeById(id);
        return ResultUtil.success();
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(@ModelAttribute StudentWorkVo vo, @ModelAttribute PageVo pageVo){
        System.out.println(vo);
        final LambdaQueryWrapper<StudentWork> lambda = new QueryWrapper<StudentWork>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<StudentWork> page = iStudentWorkService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<StudentWork> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<StudentWork> lambda, StudentWorkVo vo) {
        if(vo==null) {
            return;
        }

        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(StudentWork::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(StudentWork::getCreateTime, vo.getEndTime());
        }
        lambda.eq(StudentWork::getDelFlag,0);
        lambda.orderByDesc(StudentWork::getCreateTime);
    }



}
